package com.pz.gulimall.product.controller;

import java.util.Arrays;
import java.util.Map;

import com.pz.gulimall.common.valid.AddGroup;
import com.pz.gulimall.common.valid.UpdateGroup;
import com.pz.gulimall.product.service.impl.BrandServiceImpl;
import com.pz.gulimall.product.service.impl.CategoryBrandRelationServiceImpl;
import com.pz.gulimall.product.service.impl.FileUploadService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.pz.gulimall.product.entity.BrandEntity;
import com.pz.gulimall.product.service.BrandService;
import com.pz.gulimall.common.utils.PageUtils;
import com.pz.gulimall.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;


/**
 * 品牌
 *
 * @author PZ
 * @email pzs@gmail.com
 * @date 2024-02-01 20:22:28
 */
@RestController
@Slf4j
@RequestMapping("product/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;
    @Autowired
    private BrandServiceImpl brandServiceImpl;
    @Resource
    private FileUploadService fileService;
    @Resource
    private CategoryBrandRelationServiceImpl categoryBrandRelationService;

    @PostMapping("/uploadfile")
    public R uploadSingleFile(@Valid @RequestParam(value = "img")MultipartFile img){
        if(img.isEmpty()){
            log.error("uploadSingleFile --- fail");
            return R.error("empty file");
        }
        return fileService.uploadFile(img, img.getOriginalFilename());
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    @RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@Validated(AddGroup.class) @RequestBody  BrandEntity brand){
        brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@Validated(UpdateGroup.class) @RequestBody BrandEntity brand){
            brandService.updateById(brand);
        categoryBrandRelationService.updateWhenBrandChange(brand);
            return R.ok();
    }

    /**
     * 修改显示状态
     */
    @RequestMapping("/update/status")
    public R updateStatus(@RequestBody BrandEntity brand){
        brandService.updateById(brand);
        return R.ok();
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
